package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;
import net.tsz.afinal.exception.DbException;
import android.content.Context;
import com.heliang.dataaccess.bean.ContactBean;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.dao.MemberDao;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

public class MemberDaoImpl extends BaseDao implements MemberDao {

    public MemberDaoImpl(Context context) {
        super(context);
    }

    /**
     * 插入数据
     * @param contact
     * @return
     */
    public boolean save(ContactBean contact) throws DaoException {
        try {
            finalDb.save(contact);
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001, "MemberDaoImpl.save()"
                + "批量会员信息失败信息失败！");
            return false;
        }
    }

    /**
     * 删除会员
     * @param membername
     * @return
     */
    public boolean deleteByIds(Integer[] idArray) throws DaoException {
        try {
            String strWhere = " ID IN ( ";
            for (int i = 0; i < idArray.length; i++) {
                if (i != (idArray.length - 1)) {
                    strWhere += idArray[i] + ",";
                } else {
                    strWhere += idArray[i] + ")";
                }
            }

            finalDb.deleteByWhere(ContactBean.class, strWhere);
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001,
                "MemberDaoImpl.deleteByIds()" + "根据ID删除会员信息失败信息失败！");
            return false;
        }
    }

    /**
     * 根据名字查询会员
     * @param membername
     * @return
     */
    public List<ContactBean> queryBymemberName(String memberName) throws DaoException {
        List<ContactBean> contacts = new ArrayList<ContactBean>();
        try {
            contacts = finalDb.findAllByWhere(ContactBean.class, " userName = " + memberName + " ");
            return contacts;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004,
                "MemberDaoImpl.queryBymemberName()" + "根据会员姓名查询会员信息失败信息失败！");
            return null;
        }
    }

    /**
     * 修改数据
     * @param member
     * @return
     */
    public boolean update(ContactBean contact) throws DaoException {
        try {
            finalDb.update(contact);
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_UPDATE_ERROR_003,
                "MemberDaoImpl.queryBymemberName()" + "修改会员信息失败！");
            return false;
        }
    }
}
